
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="zh_Hans">
  <head>
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>数据库迁移操作 &#8212; Django 3.2.11.dev 文档</title>
    <link rel="stylesheet" href="../../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
    <script type="text/javascript" id="documentation_options" data-url_root="../../../" src="../../../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../../_static/doctools.js"></script>
    <script type="text/javascript" src="../../../_static/language_data.js"></script>
    <link rel="index" title="索引" href="../../../genindex.html" />
    <link rel="search" title="搜索" href="../../../search.html" />
    <link rel="next" title="全文搜索" href="search.html" />
    <link rel="prev" title="PostgreSQL 特有查找" href="lookups.html" />



 
<script src="../../../templatebuiltins.js"></script>
<script>
(function($) {
    if (!django_template_builtins) {
       // templatebuiltins.js missing, do nothing.
       return;
    }
    $(document).ready(function() {
        // Hyperlink Django template tags and filters
        var base = "../../templates/builtins.html";
        if (base == "#") {
            // Special case for builtins.html itself
            base = "";
        }
        // Tags are keywords, class '.k'
        $("div.highlight\\-html\\+django span.k").each(function(i, elem) {
             var tagname = $(elem).text();
             if ($.inArray(tagname, django_template_builtins.ttags) != -1) {
                 var fragment = tagname.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>");
             }
        });
        // Filters are functions, class '.nf'
        $("div.highlight\\-html\\+django span.nf").each(function(i, elem) {
             var filtername = $(elem).text();
             if ($.inArray(filtername, django_template_builtins.tfilters) != -1) {
                 var fragment = filtername.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>");
             }
        });
    });
})(jQuery);</script>

  </head><body>

    <div class="document">
  <div id="custom-doc" class="yui-t6">
    <div id="hd">
      <h1><a href="../../../index.html">Django 3.2.11.dev 文档</a></h1>
      <div id="global-nav">
        <a title="Home page" href="../../../index.html">Home</a>  |
        <a title="Table of contents" href="../../../contents.html">Table of contents</a>  |
        <a title="Global index" href="../../../genindex.html">Index</a>  |
        <a title="Module index" href="../../../py-modindex.html">Modules</a>
      </div>
      <div class="nav">
    &laquo; <a href="lookups.html" title="PostgreSQL 特有查找">previous</a>
     |
    <a href="../../index.html" title="API 参考" accesskey="U">up</a>
   |
    <a href="search.html" title="全文搜索">next</a> &raquo;</div>
    </div>

    <div id="bd">
      <div id="yui-main">
        <div class="yui-b">
          <div class="yui-g" id="ref-contrib-postgres-operations">
            
  <div class="section" id="s-database-migration-operations">
<span id="database-migration-operations"></span><h1>数据库迁移操作<a class="headerlink" href="#database-migration-operations" title="永久链接至标题">¶</a></h1>
<p>所有这些 <a class="reference internal" href="../../migration-operations.html"><span class="doc">操作</span></a> 都可以从 <code class="docutils literal notranslate"><span class="pre">django.contrib.postgres.operations</span></code> 模块中获得。</p>
<div class="section" id="s-creating-extension-using-migrations">
<span id="s-create-postgresql-extensions"></span><span id="creating-extension-using-migrations"></span><span id="create-postgresql-extensions"></span><h2>使用迁移创建扩展<a class="headerlink" href="#creating-extension-using-migrations" title="永久链接至标题">¶</a></h2>
<p>你可以使用迁移文件在数据库中创建一个 PostgreSQL 扩展。这个例子创建了一个 hstore 扩展，但同样的原理也适用于其他扩展。</p>
<p>在涉及到 <a class="reference internal" href="fields.html#django.contrib.postgres.fields.HStoreField" title="django.contrib.postgres.fields.HStoreField"><code class="xref py py-class docutils literal notranslate"><span class="pre">HStoreField</span></code></a> 的第一个 <code class="docutils literal notranslate"><span class="pre">CreateModel</span></code> 或 <code class="docutils literal notranslate"><span class="pre">AddField</span></code> 操作之前，通过添加 <a class="reference internal" href="#django.contrib.postgres.operations.HStoreExtension" title="django.contrib.postgres.operations.HStoreExtension"><code class="xref py py-class docutils literal notranslate"><span class="pre">HStoreExtension</span></code></a> 操作的迁移，在 PostgreSQL 中设置 hstore 扩展。例如：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.contrib.postgres.operations</span> <span class="kn">import</span> <span class="n">HStoreExtension</span>

<span class="k">class</span> <span class="nc">Migration</span><span class="p">(</span><span class="n">migrations</span><span class="o">.</span><span class="n">Migration</span><span class="p">):</span>
    <span class="o">...</span>

    <span class="n">operations</span> <span class="o">=</span> <span class="p">[</span>
        <span class="n">HStoreExtension</span><span class="p">(),</span>
        <span class="o">...</span>
    <span class="p">]</span>
</pre></div>
</div>
<p>如果扩展名已经存在，该操作会跳过添加。</p>
<p>对于大多数扩展来说，这需要一个具有超级用户权限的数据库用户。如果 Django 数据库的用户没有相应的权限，你就必须在 Django 迁移之外用一个有权限的用户创建扩展。在这种情况下，连接到你的 Django 数据库，并运行查询 <code class="docutils literal notranslate"><span class="pre">CREATE</span> <span class="pre">EXTENSION</span> <span class="pre">IF</span> <span class="pre">NOT</span> <span class="pre">EXISTS</span> <span class="pre">hstore;</span></code>。</p>
<div class="versionchanged">
<span class="title">Changed in Django 3.2:</span> <p>在旧版本中，没有检查扩展是否预先存在。</p>
</div>
</div>
<div class="section" id="s-createextension">
<span id="createextension"></span><h2><code class="docutils literal notranslate"><span class="pre">CreateExtension</span></code><a class="headerlink" href="#createextension" title="永久链接至标题">¶</a></h2>
<dl class="class">
<dt id="django.contrib.postgres.operations.CreateExtension">
<em class="property">class </em><code class="descname">CreateExtension</code>(<em>name</em>)<a class="headerlink" href="#django.contrib.postgres.operations.CreateExtension" title="永久链接至目标">¶</a></dt>
<dd><p>一个安装 PostgreSQL 扩展的 <code class="docutils literal notranslate"><span class="pre">Operation</span></code> 子类。对于普通的扩展，请使用下面一个更具体的子类。</p>
<dl class="attribute">
<dt id="django.contrib.postgres.operations.CreateExtension.name">
<code class="descname">name</code><a class="headerlink" href="#django.contrib.postgres.operations.CreateExtension.name" title="永久链接至目标">¶</a></dt>
<dd><p>这是一个必要的参数。要安装的扩展名。</p>
</dd></dl>

</dd></dl>

</div>
<div class="section" id="s-bloomextension">
<span id="bloomextension"></span><h2><code class="docutils literal notranslate"><span class="pre">BloomExtension</span></code><a class="headerlink" href="#bloomextension" title="永久链接至标题">¶</a></h2>
<dl class="class">
<dt id="django.contrib.postgres.operations.BloomExtension">
<em class="property">class </em><code class="descname">BloomExtension</code><a class="headerlink" href="#django.contrib.postgres.operations.BloomExtension" title="永久链接至目标">¶</a></dt>
<dd><div class="versionadded">
<span class="title">New in Django 3.1.</span> </div>
<p>安装 <code class="docutils literal notranslate"><span class="pre">bloom</span></code> 扩展。</p>
</dd></dl>

</div>
<div class="section" id="s-btreeginextension">
<span id="btreeginextension"></span><h2><code class="docutils literal notranslate"><span class="pre">BtreeGinExtension</span></code><a class="headerlink" href="#btreeginextension" title="永久链接至标题">¶</a></h2>
<dl class="class">
<dt id="django.contrib.postgres.operations.BtreeGinExtension">
<em class="property">class </em><code class="descname">BtreeGinExtension</code><a class="headerlink" href="#django.contrib.postgres.operations.BtreeGinExtension" title="永久链接至目标">¶</a></dt>
<dd><p>安装 <code class="docutils literal notranslate"><span class="pre">btree_gin</span></code> 扩展。</p>
</dd></dl>

</div>
<div class="section" id="s-btreegistextension">
<span id="btreegistextension"></span><h2><code class="docutils literal notranslate"><span class="pre">BtreeGistExtension</span></code><a class="headerlink" href="#btreegistextension" title="永久链接至标题">¶</a></h2>
<dl class="class">
<dt id="django.contrib.postgres.operations.BtreeGistExtension">
<em class="property">class </em><code class="descname">BtreeGistExtension</code><a class="headerlink" href="#django.contrib.postgres.operations.BtreeGistExtension" title="永久链接至目标">¶</a></dt>
<dd><p>安装 <code class="docutils literal notranslate"><span class="pre">btree_gist</span></code> 扩展。</p>
</dd></dl>

</div>
<div class="section" id="s-citextextension">
<span id="citextextension"></span><h2><code class="docutils literal notranslate"><span class="pre">CITextExtension</span></code><a class="headerlink" href="#citextextension" title="永久链接至标题">¶</a></h2>
<dl class="class">
<dt id="django.contrib.postgres.operations.CITextExtension">
<em class="property">class </em><code class="descname">CITextExtension</code><a class="headerlink" href="#django.contrib.postgres.operations.CITextExtension" title="永久链接至目标">¶</a></dt>
<dd><p>安装 <code class="docutils literal notranslate"><span class="pre">citext</span></code> 扩展。</p>
</dd></dl>

</div>
<div class="section" id="s-cryptoextension">
<span id="cryptoextension"></span><h2><code class="docutils literal notranslate"><span class="pre">CryptoExtension</span></code><a class="headerlink" href="#cryptoextension" title="永久链接至标题">¶</a></h2>
<dl class="class">
<dt id="django.contrib.postgres.operations.CryptoExtension">
<em class="property">class </em><code class="descname">CryptoExtension</code><a class="headerlink" href="#django.contrib.postgres.operations.CryptoExtension" title="永久链接至目标">¶</a></dt>
<dd><p>安装 <code class="docutils literal notranslate"><span class="pre">pgcrypto</span></code> 扩展。</p>
</dd></dl>

</div>
<div class="section" id="s-hstoreextension">
<span id="hstoreextension"></span><h2><code class="docutils literal notranslate"><span class="pre">HStoreExtension</span></code><a class="headerlink" href="#hstoreextension" title="永久链接至标题">¶</a></h2>
<dl class="class">
<dt id="django.contrib.postgres.operations.HStoreExtension">
<em class="property">class </em><code class="descname">HStoreExtension</code><a class="headerlink" href="#django.contrib.postgres.operations.HStoreExtension" title="永久链接至目标">¶</a></dt>
<dd><p>安装 <code class="docutils literal notranslate"><span class="pre">hstore</span></code> 扩展，并设置连接来解释 hstore 数据，以便在后续迁移中使用。</p>
</dd></dl>

</div>
<div class="section" id="s-trigramextension">
<span id="trigramextension"></span><h2><code class="docutils literal notranslate"><span class="pre">TrigramExtension</span></code><a class="headerlink" href="#trigramextension" title="永久链接至标题">¶</a></h2>
<dl class="class">
<dt id="django.contrib.postgres.operations.TrigramExtension">
<em class="property">class </em><code class="descname">TrigramExtension</code><a class="headerlink" href="#django.contrib.postgres.operations.TrigramExtension" title="永久链接至目标">¶</a></dt>
<dd><p>安装 <code class="docutils literal notranslate"><span class="pre">pg_trgm</span></code> 扩展。</p>
</dd></dl>

</div>
<div class="section" id="s-unaccentextension">
<span id="unaccentextension"></span><h2><code class="docutils literal notranslate"><span class="pre">UnaccentExtension</span></code><a class="headerlink" href="#unaccentextension" title="永久链接至标题">¶</a></h2>
<dl class="class">
<dt id="django.contrib.postgres.operations.UnaccentExtension">
<em class="property">class </em><code class="descname">UnaccentExtension</code><a class="headerlink" href="#django.contrib.postgres.operations.UnaccentExtension" title="永久链接至目标">¶</a></dt>
<dd><p>安装 <code class="docutils literal notranslate"><span class="pre">unaccent</span></code> 扩展。</p>
</dd></dl>

</div>
<div class="section" id="s-managing-collations-using-migrations">
<span id="s-manage-postgresql-collations"></span><span id="managing-collations-using-migrations"></span><span id="manage-postgresql-collations"></span><h2>使用迁移来管理整理<a class="headerlink" href="#managing-collations-using-migrations" title="永久链接至标题">¶</a></h2>
<div class="versionadded">
<span class="title">New in Django 3.2.</span> </div>
<p>如果你需要使用你的操作系统提供的特定字节序来过滤或排序一个列，但 PostgreSQL 没有提供，你可以使用迁移文件来管理数据库中的字节序。然后，这些字节序可以在 <a class="reference internal" href="../../models/fields.html#django.db.models.CharField" title="django.db.models.CharField"><code class="xref py py-class docutils literal notranslate"><span class="pre">CharField</span></code></a>、<a class="reference internal" href="../../models/fields.html#django.db.models.TextField" title="django.db.models.TextField"><code class="xref py py-class docutils literal notranslate"><span class="pre">TextField</span></code></a> 以及它们的子类上使用 <code class="docutils literal notranslate"><span class="pre">db_collation</span></code> 参数。</p>
<p>例如，为德国电话簿的排序创建一个字节序：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.contrib.postgres.operations</span> <span class="kn">import</span> <span class="n">CreateCollation</span>

<span class="k">class</span> <span class="nc">Migration</span><span class="p">(</span><span class="n">migrations</span><span class="o">.</span><span class="n">Migration</span><span class="p">):</span>
    <span class="o">...</span>

    <span class="n">operations</span> <span class="o">=</span> <span class="p">[</span>
        <span class="n">CreateCollation</span><span class="p">(</span>
            <span class="s1">&#39;german_phonebook&#39;</span><span class="p">,</span>
            <span class="n">provider</span><span class="o">=</span><span class="s1">&#39;icu&#39;</span><span class="p">,</span>
            <span class="n">locale</span><span class="o">=</span><span class="s1">&#39;und-u-ks-level2&#39;</span><span class="p">,</span>
        <span class="p">),</span>
        <span class="o">...</span>
    <span class="p">]</span>
</pre></div>
</div>
<dl class="class">
<dt id="django.contrib.postgres.operations.CreateCollation">
<em class="property">class </em><code class="descname">CreateCollation</code>(<em>name</em>, <em>locale</em>, <em>*</em>, <em>provider='libc'</em>, <em>deterministic=True</em>)<a class="headerlink" href="#django.contrib.postgres.operations.CreateCollation" title="永久链接至目标">¶</a></dt>
<dd><p>用给定的 <code class="docutils literal notranslate"><span class="pre">name</span></code>、<code class="docutils literal notranslate"><span class="pre">locale</span></code> 和 <code class="docutils literal notranslate"><span class="pre">provider</span></code> 创建一个字节序。</p>
<p>将 <code class="docutils literal notranslate"><span class="pre">deterministic</span></code> 参数设置为 <code class="docutils literal notranslate"><span class="pre">False</span></code> 以创建一个非确定的字节序，例如用于不区分大小写的过滤。</p>
</dd></dl>

<dl class="class">
<dt id="django.contrib.postgres.operations.RemoveCollation">
<em class="property">class </em><code class="descname">RemoveCollation</code>(<em>name</em>, <em>locale</em>, <em>*</em>, <em>provider='libc'</em>, <em>deterministic=True</em>)<a class="headerlink" href="#django.contrib.postgres.operations.RemoveCollation" title="永久链接至目标">¶</a></dt>
<dd><p>移除名为 <code class="docutils literal notranslate"><span class="pre">name</span></code> 的字节序。</p>
<p>当反查时，这是用提供的 <code class="docutils literal notranslate"><span class="pre">locale</span></code>、<code class="docutils literal notranslate"><span class="pre">provider</span></code> 和 <code class="docutils literal notranslate"><span class="pre">deterministic</span></code> 参数创建一个字节序。因此，需要 <code class="docutils literal notranslate"><span class="pre">locale</span></code> 来使这个操作可逆。</p>
</dd></dl>

<div class="admonition-restrictions admonition">
<p class="first admonition-title">限制条件</p>
<p>PostgreSQL 9.6 只有  <code class="docutils literal notranslate"><span class="pre">'libc'</span></code> 支持。</p>
<p class="last">只有 PostgreSQL 12+ 支持非确定的的字节序。</p>
</div>
</div>
<div class="section" id="s-concurrent-index-operations">
<span id="concurrent-index-operations"></span><h2>并发索引操作<a class="headerlink" href="#concurrent-index-operations" title="永久链接至标题">¶</a></h2>
<p>PostgreSQL 支持 <code class="docutils literal notranslate"><span class="pre">CREATE</span> <span class="pre">INDEX</span></code> 和 <code class="docutils literal notranslate"><span class="pre">DROP</span> <span class="pre">INDEX</span></code> 语句中的 <code class="docutils literal notranslate"><span class="pre">CONCURRENTLY</span></code> 选项，以增加和删除索引而不锁定写入。这个选项对于在实际生产的数据库中添加或删除索引非常有用。</p>
<dl class="class">
<dt id="django.contrib.postgres.operations.AddIndexConcurrently">
<em class="property">class </em><code class="descname">AddIndexConcurrently</code>(<em>model_name</em>, <em>index</em>)<a class="headerlink" href="#django.contrib.postgres.operations.AddIndexConcurrently" title="永久链接至目标">¶</a></dt>
<dd><p>就像 <a class="reference internal" href="../../migration-operations.html#django.db.migrations.operations.AddIndex" title="django.db.migrations.operations.AddIndex"><code class="xref py py-class docutils literal notranslate"><span class="pre">AddIndex</span></code></a> 一样，但是使用 <code class="docutils literal notranslate"><span class="pre">CONCURRENTLY</span></code> 选项创建索引。这在使用这个选项时有一些注意事项，参见 <a class="reference external" href="https://www.postgresql.org/docs/current/sql-createindex.html#SQL-CREATEINDEX-CONCURRENTLY">PostgreSQL 关于并发建立索引的文档</a> 。</p>
</dd></dl>

<dl class="class">
<dt id="django.contrib.postgres.operations.RemoveIndexConcurrently">
<em class="property">class </em><code class="descname">RemoveIndexConcurrently</code>(<em>model_name</em>, <em>name</em>)<a class="headerlink" href="#django.contrib.postgres.operations.RemoveIndexConcurrently" title="永久链接至目标">¶</a></dt>
<dd><p>就像 <a class="reference internal" href="../../migration-operations.html#django.db.migrations.operations.RemoveIndex" title="django.db.migrations.operations.RemoveIndex"><code class="xref py py-class docutils literal notranslate"><span class="pre">RemoveIndex</span></code></a> 一样，但是使用 <code class="docutils literal notranslate"><span class="pre">CONCURRENTLY</span></code> 选项来删除索引。这在使用这个选项时有一些注意事项，请看 <a class="reference external" href="https://www.postgresql.org/docs/current/sql-dropindex.html">PostgreSQL 文档</a> 。</p>
</dd></dl>

<div class="admonition note">
<p class="first admonition-title">注解</p>
<p class="last">在事务中不支持 <code class="docutils literal notranslate"><span class="pre">CONCURRENTLY</span></code> 选项（见 <a class="reference internal" href="../../../howto/writing-migrations.html#non-atomic-migrations"><span class="std std-ref">非原子性迁移</span></a>）。</p>
</div>
</div>
</div>


          </div>
        </div>
      </div>
      
        
          <div class="yui-b" id="sidebar">
            
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <h3><a href="../../../contents.html">Table of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">数据库迁移操作</a><ul>
<li><a class="reference internal" href="#creating-extension-using-migrations">使用迁移创建扩展</a></li>
<li><a class="reference internal" href="#createextension"><code class="docutils literal notranslate"><span class="pre">CreateExtension</span></code></a></li>
<li><a class="reference internal" href="#bloomextension"><code class="docutils literal notranslate"><span class="pre">BloomExtension</span></code></a></li>
<li><a class="reference internal" href="#btreeginextension"><code class="docutils literal notranslate"><span class="pre">BtreeGinExtension</span></code></a></li>
<li><a class="reference internal" href="#btreegistextension"><code class="docutils literal notranslate"><span class="pre">BtreeGistExtension</span></code></a></li>
<li><a class="reference internal" href="#citextextension"><code class="docutils literal notranslate"><span class="pre">CITextExtension</span></code></a></li>
<li><a class="reference internal" href="#cryptoextension"><code class="docutils literal notranslate"><span class="pre">CryptoExtension</span></code></a></li>
<li><a class="reference internal" href="#hstoreextension"><code class="docutils literal notranslate"><span class="pre">HStoreExtension</span></code></a></li>
<li><a class="reference internal" href="#trigramextension"><code class="docutils literal notranslate"><span class="pre">TrigramExtension</span></code></a></li>
<li><a class="reference internal" href="#unaccentextension"><code class="docutils literal notranslate"><span class="pre">UnaccentExtension</span></code></a></li>
<li><a class="reference internal" href="#managing-collations-using-migrations">使用迁移来管理整理</a></li>
<li><a class="reference internal" href="#concurrent-index-operations">并发索引操作</a></li>
</ul>
</li>
</ul>

  <h4>上一个主题</h4>
  <p class="topless"><a href="lookups.html"
                        title="上一章">PostgreSQL 特有查找</a></p>
  <h4>下一个主题</h4>
  <p class="topless"><a href="search.html"
                        title="下一章">全文搜索</a></p>
  <div role="note" aria-label="source link">
    <h3>本页</h3>
    <ul class="this-page-menu">
      <li><a href="../../../_sources/ref/contrib/postgres/operations.txt"
            rel="nofollow">显示源代码</a></li>
    </ul>
   </div>
<div id="searchbox" style="display: none" role="search">
  <h3>快速搜索</h3>
    <div class="searchformwrapper">
    <form class="search" action="../../../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="转向" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    </div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
              <h3>Last update:</h3>
              <p class="topless">12月 07, 2021</p>
          </div>
        
      
    </div>

    <div id="ft">
      <div class="nav">
    &laquo; <a href="lookups.html" title="PostgreSQL 特有查找">previous</a>
     |
    <a href="../../index.html" title="API 参考" accesskey="U">up</a>
   |
    <a href="search.html" title="全文搜索">next</a> &raquo;</div>
    </div>
  </div>

      <div class="clearer"></div>
    </div>
  </body>
</html>